<?php

namespace app\admin\middleware;
use think\facade\Config;
use app\admin\extend\firebase\Jwt;
class CheckAuth
{
    public function handle($request, \Closure $next)
    {

        $controller = $request->controller(true);
        //方法
        $action = $request->action(true);

        // 不需要登录处理
        $noNeedLoginConfig = Config::get('no_need_login');
        if (isset($noNeedLoginConfig[$controller])) {
            if ($noNeedLoginConfig[$controller] === '*' || in_array($action,$noNeedLoginConfig[$controller])) {
                return $next($request);
            }
        }

        $token = $request->header('access-token');
        $jwt = new Jwt();
        $userInfo = $jwt->decrypt($token);
        if ($userInfo === false) {
            return show_serve(httpStatusCode:401);
        }
        $request->userInfo = $userInfo['data'];

        // 不需要权限处理
//        $noNeedAuthConfig = Config::get('no_need_auth');
//        if (isset($noNeedAuthConfig[$controller])) {
//            if ($noNeedAuthConfig[$controller] === '*' || in_array($action,$noNeedAuthConfig[$controller])) {
//                return $next($request);
//            }
//        }

//        $ruleModel = new RuleModel();
//        $auth = $ruleModel->routeVerify($userInfo['data']->roleId,$controller.'/'.$action);
//        if (!$auth) {
//            return show(code: 400, message: '没有权限');
//        }



        return $next($request);
    }

}